package com.ogawa.chair7808.ui.activity;

import android.os.Handler;

import androidx.fragment.app.Fragment;

import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.ogawa.base.Constant.CommmandNum;
import com.ogawa.base.utils.CommonUtil;
import com.ogawa.chair7808.R;
import com.ogawa.chair7808.bean.Massage7808Armchair;
import com.ogawa.chair7808.manager.DataManager;
import com.ogawa.chair7808.ui.fragment.AirPressureMassageFragment;
import com.ogawa.chair7808.ui.fragment.FragmentAdapter;
import com.ogawa.chair7808.ui.fragment.MassageTechniqueFragment;
import com.ogawa.chair7808.widget.CustomViewPager;

import java.util.ArrayList;
import java.util.List;



/**
 * 高级按摩
 */
public class AdvancedMassageActivity7808 extends BaseMqtt7808Activity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener
//        , IAdvancedMassageView7506
//        , CloseAdvance.onTimeFinish, TimeObserver.onTimeUpdataLis
{


    public static final int TAG_ANMOSHOUFA = 0;
    public static final int TAG_QIYAANMO = 1;
    /**
     * 存放内容页面
     */
    private ArrayList<Fragment> contentPage;
    private RadioButton tableft;
    private RadioButton tabright;
    private ImageView mIvStart;
    private Fragment mFrag;
    private ImageView mIvPower;
    private String model;
    private RelativeLayout mRlTop;
    private FragmentAdapter mAdapter;//适配器
    private List<String> mTabs = new ArrayList<>();//存放标签页标题

//    private AdvancedMassagePressenter7506 mPresenter;
    private MassageTechniqueFragment mFrangment1;
    private AirPressureMassageFragment mFrangment2;
    private CustomViewPager viewPager;
//    private boolean isFinishShow;
    private boolean isPowerOn;
    private String mMin, mSec;
    private Handler handler;

    @Override
    public void initTitleBar() {
        super.initTitleBar();
        showTitleBar(false);
        setBackGround(R.mipmap.bg);

    }

    @Override
    public void initView(int layoutId) {
        super.initView(layoutId);
        handler = new Handler();
        initFragment();
        initView();
        iniTitle();
//        mPresenter = new AdvancedMassagePressenter7506(this);
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                mPresenter.getTimes();
//            }
//        }, 500);
//        CloseAdvance.getInst().registerObserver(this);
//        TimeObserver.getInst().registerObserver(this);
    }


    private void initFragment() {
        contentPage = new ArrayList<Fragment>();
        mFrangment1 = new MassageTechniqueFragment();
        mFrangment2 = new AirPressureMassageFragment();

        contentPage.add(mFrangment1);
        contentPage.add(mFrangment2);
        mTabs.add("按摩手法");
        mTabs.add("气压按摩");


    }

    private void iniTitle() {
        mRlTop = findViewById(R.id.re);
        CommonUtil.setMargins(mRlTop, 0, CommonUtil.getStatusBarHeight(this), 0, 0);
        findViewById(R.id.chair_back).setOnClickListener(this);
        mIvPower = findViewById(R.id.chair_power_close);
        mIvPower.setOnClickListener(this);
        mIvPower.setSelected(DataManager.getInst().isOpen());
    }


    private void initView() {

        model = getIntent().getStringExtra("model");
        viewPager = findViewById(R.id.viewpager);
        viewPager.setOffscreenPageLimit(1);
        mAdapter = new FragmentAdapter(getSupportFragmentManager(), contentPage, mTabs);
        viewPager.setAdapter(mAdapter);//给ViewPager设置适配器
        viewPager.setOnViewPagerScrollAction(new CustomViewPager.setOnViewPagerScrollAction() {
            @Override
            public boolean unAbleScroll() {
                return true;
            }
        });
        tableft = findViewById(R.id.tableft);
        tabright = findViewById(R.id.tabright);

        isPowerOn = DataManager.getInst().isOpen();
        tableft.setOnCheckedChangeListener(this);
        tableft.setTag(TAG_ANMOSHOUFA);
        tableft.setChecked(true);


        tabright.setOnCheckedChangeListener(this);
        tabright.setTag(TAG_QIYAANMO);

        mIvStart = findViewById(R.id.iv_switch);
        mIvStart.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.chair_back) {
            finish();

        } else if (i == R.id.iv_switch) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getInst().sendCommond(CommmandNum.PAUSE,"5");
            }

        } else if (i == R.id.chair_power_close) {
            if (isPowerOn) {
                DataManager.getInst().sendCommond(CommmandNum.CLOSE,"0");
            } else {
                DataManager.getInst().sendCommond(CommmandNum.OPEN,"1");
            }
        }
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (isChecked) {
            int i = buttonView.getId();
            if (i == R.id.tableft) {
                viewPager.setCurrentItem(0);
                tableft.setBackgroundResource(R.mipmap.tab_letf);
                tabright.setBackgroundResource(0);

            } else if (i == R.id.tabright) {
                tabright.setBackgroundResource(R.mipmap.tab_letf);
                tableft.setBackgroundResource(0);
                viewPager.setCurrentItem(1);

            } else {
            }

        }
    }


    @Override
    public int setLayoutId() {
        return R.layout.activity_advanced_massage_7808;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        mPresenter.onDestory();
    }

//    @Override
//    public void isPause(boolean isPause) {
//        mIvStart.setSelected(isPause);
//        if (isPause) {
//            mFrangment1.pauseTime();
//            mFrangment2.pauseTime();
//        } else {
//            mFrangment1.reStarTime();
//            mFrangment2.reStarTime();
//
//        }
//    }

//    @Override
//    public void getTime(String min, String sec) {
//        if (!(min.equals("0") && sec.equals("0"))) {
//            mFrangment1.initTime(min, sec);
//            mFrangment2.initTime(min, sec);
//        }
//
//    }
//
//    @Override
//    public void toAutoActivity(String name) {
//        cancleFinishDialog();
//        CloseScanAndAdjust.getInst().noticeonClose();
//        Activity7808Utils.startProMasDetailActivity(this, name);
//
//        this.finish();
//    }


//    @Override
//    public void startToScan() {
//        Activity7808Utils.startScanShoulderActivity(this);
//
//    }
//
//    @Override
//    public void startAdjust() {
//        Activity7808Utils.startShouldersAdjustmentActivity(this);
//    }
//
//    @Override
//    public void handRunning() {
//        cancleFinishDialog();
//    }


//    @Override
//    public void onTimeFinish() {
//        DataManager.getInst().sendCommond(CommmandNum.FINISH,"4");
//        Log.e("onTimeFinish", isFinishDialogShowing() + "");
//        if (!isFinishShow) {
//            isFinishShow = true;
//            showFinishDialog(this, "-1", new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    isFinishShow = false;
//                    cancleFinishDialog();
//                }
//            });
//        }
//
//
//    }
//
//    @Override
//    public void onupdataTime(String min, String sec) {
//        mMin = min;
//        mSec = sec;
//        mFrangment1.upDataTime(min, sec);
//        mFrangment2.upDataTime(min, sec);
//    }

    @Override
    public void changeViewByMqttMessage() {
        if ("3".equals(DataManager.getInst().getDevice7808StateBean().getRunningStatus())){
            mIvStart.setSelected(true);
        }else {
            mIvStart.setSelected(false);
        }
    }
}
